package com.zzqstart.bannerservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzqstart.bannerservice.entity.Banner;
import com.zzqstart.bannerservice.entity.QueryBanner;
import com.zzqstart.bannerservice.mapper.BannerMapper;
import com.zzqstart.bannerservice.service.BannerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 首页banner表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2023-03-06
 */
@Service
public class BannerServiceImpl extends ServiceImpl<BannerMapper, Banner> implements BannerService {

    @Override
    public List<Banner> getPartBanner() {

        QueryWrapper<Banner> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("sort", "create_time");
        wrapper.eq("is_deleted", "0");

        List<Banner> bannerList = baseMapper.selectList(wrapper);
        return bannerList;
    }

    @Override
    public Map<String, Object> pageBanner(Long page, Long limit, QueryBanner banner) {
        QueryWrapper<Banner> wrapper = new QueryWrapper<>();

        if (!StringUtils.isEmpty(banner)) {
            if (banner.getIsSort()) {
                wrapper.orderByAsc("sort");
            }
            if (!StringUtils.isEmpty(banner.getLinkUrl())) {
                wrapper.like("link_url",banner.getLinkUrl());
            }
            if (!StringUtils.isEmpty(banner.getIsDeleted())) {
                wrapper.eq("is_deleted", banner.getIsDeleted());
            }
            if (!StringUtils.isEmpty(banner.getTitle())) {
                wrapper.like("title", banner.getTitle());
            }
            if (!StringUtils.isEmpty(banner.getCreateTime())) {
                wrapper.ge("create_time", banner.getCreateTime());
            }
        }
        Page<Banner> bannerPage = new Page<>(page, limit);

        baseMapper.selectPage(bannerPage, wrapper);
        Map<String, Object> map = new HashMap<>();

        List<Banner> items = bannerPage.getRecords();
        long total = bannerPage.getTotal();
        long size = bannerPage.getSize();

        map.put("list",items);
        map.put("total",total);
        map.put("size",size);

        return map;
    }


}
